package com.market.web.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.market.domain.Location;
import com.market.web.model.LocationModel;
@Component("locationConverter")
@Scope("session")
public class LocationConverter implements Converter {
	@Autowired
	private LocationModel locationModel;	

	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		Location loc = new Location();
		loc.setId(Long.parseLong(arg2));
		return locationModel.lookUp(loc);
	}

	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		if (arg2 instanceof Location) {
			Location loc = (Location) arg2;
			return loc.getId().toString();
		}
		return null;
	}

}
